/* Simple applet demonstrating access to JavaScript
   by Danny Goodman  (http://www.dannyg.com)
   
   (To modify and recompile, rename file to 'JtoJSDemo.java')
*/
import java.awt.*;
import netscape.javascript.*;

public class JtoJSDemo extends java.applet.Applet {
    private JSObject mainwin, subwin;
    private Button newWinButton, toggleButton;

    public void init() {
        setBackground(Color.white);
        newWinButton = new Button("New Browser Window");
        toggleButton = new Button("Toggle SubWindow Color");
        this.add(newWinButton);
        this.add(toggleButton);
        mainwin = JSObject.getWindow(this);
    }

    public void start() {
        mainwin.eval("document.indicator.running.value = 'Yes'");
    }

    public boolean action(Event evt, Object arg) {
        if (evt.target instanceof Button) {
            if (evt.target == newWinButton) {
                doNewWindow();
            } else if (evt.target == toggleButton) {
                toggleColor();
            }
        }
        return true;
    }

    void doNewWindow() {
        subwin = (JSObject) mainwin.eval("window.open('','fromApplet','HEIGHT=200,WIDTH=200')");
        subwin.eval("document.write('<HTML><BODY BGCOLOR=white>Howdy from the applet!</BODY></HTML>')");
        subwin.eval("document.close()");
    }

    void toggleColor() {
        if (subwin != null) {
            JSObject arg[] = {subwin};
            mainwin.call("toggleSubWindowColor", arg);
        }
    }

}
